package org.jeecg.modules.app.user.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fasterxml.jackson.databind.ObjectMapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.ApiLogin;
import org.jeecg.common.util.oConvertUtils;
import org.jeecg.modules.app.user.vo.ResultUser;
import org.jeecg.modules.power.user.entity.YlUser;
import org.jeecg.modules.power.user.entity.YlUserDeposit;
import org.jeecg.modules.power.user.service.IYlUserDepositService;
import org.jeecg.modules.power.user.service.IYlUserService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @BelongsProject: jeecg-boot-parent
 * @BelongsPackage: org.jeecg.modules.app.user.controller
 * @Author: mi
 * @CreateTime: 2024-07-12 14:40
 * @Description:
 */
@CrossOrigin
@Api(tags = "小程序用户相关接口")
@RestController
@RequestMapping("/api/programUser")
public class ProgramUserController {
    @Autowired
    private IYlUserService ylUserService;
    @Autowired
    private IYlUserDepositService ylUserDepositService;//用户押金明细


    @ApiLogin
    @ApiOperation("小程序获取用户信息")
    @GetMapping("/getProgramUserEnt")
    public Result<ResultUser>getProgramUserEnt(HttpServletRequest request){
        Result<ResultUser> result = new Result<ResultUser>();
        YlUser user=new ObjectMapper().convertValue(request.getAttribute("user"),YlUser.class);//转对象 用户登陆对象
        ResultUser resultUser= new  ResultUser();
        BeanUtils.copyProperties(user, resultUser);

        resultUser.setAvatar(oConvertUtils.isEmpty(user.getAvatar()) ?"": user.getAvatar());
        resultUser.setEndTime(oConvertUtils.isEmpty(user.getEndTime()) ? null: user.getEndTime());
        result.setCode(200);
        result.setResult(resultUser);
        return result;
    }


    //编辑用户 更换头像  更换昵称 更换头像
    @ApiLogin
    @ApiOperation(value="小程序编辑用户 更换头像  更换昵称 更换头像")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "tel", value = "手机号", required = false),
            @ApiImplicitParam(name = "avatar", value = "头像", required = false),
            @ApiImplicitParam(name = "nickname", value = "昵称", required = false),

    })
    @PostMapping("editUserEnt")
    public Result<?>editUserEnt(HttpServletRequest request,String tel,String avatar,String nickname){
        YlUser user=new ObjectMapper().convertValue(request.getAttribute("user"),YlUser.class);//转对象 用户登陆对象
        Integer i=0;//是否确认修改标示 0 不用修改  1、代表要修改
        YlUser ylUser=new YlUser();
        ylUser.setId(user.getId());
        if (oConvertUtils.isNotEmpty(tel)){
            ylUser.setTel(tel);
            i=1;
        }
        if (oConvertUtils.isNotEmpty(avatar)){
            ylUser.setAvatar(avatar);
            i=1;
        }   if (oConvertUtils.isNotEmpty(nickname)){
            ylUser.setNickname(nickname);
            i=1;
        }
        if (i==1){
            ylUserService.updateById(ylUser);
        }
        return Result.OK("修改成功");
    }
    //获取押金明细
    @ApiLogin
    @ApiOperation(value="小程序用户获取押金明细")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNo" ,value = "pageNo",required = true),
            @ApiImplicitParam(name = "pageSize" ,value = "pageSize",required = true),
    })
    @GetMapping("getUserDepositList")
    public Result<List<YlUserDeposit>>getUserDepositList(HttpServletRequest request ,	   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
                                       @RequestParam(name="pageSize", defaultValue="10") Integer pageSize){
        YlUser user=new ObjectMapper().convertValue(request.getAttribute("user"),YlUser.class);//转对象 用户登陆对象
        QueryWrapper<YlUserDeposit> queryWrapper =new QueryWrapper<YlUserDeposit>();
        queryWrapper.eq("user_id",user.getId());
        queryWrapper.orderByDesc("create_time");
        Page<YlUserDeposit> page = new Page<YlUserDeposit>(pageNo, pageSize);
        IPage<YlUserDeposit> pageList = ylUserDepositService.page(page, queryWrapper);
        return Result.OK(pageList.getRecords());
    }






}
